package org.spring.springboot.app.hard.hardinfo.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.spring.springboot.app.adv.advcombhard.domain.po.AdvCombHardPO;
import org.spring.springboot.app.adv.advcombinfo.service.IAdvCombInfoService;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.Type;
import org.spring.springboot.app.base.User;
import org.spring.springboot.app.hard.hardinfo.domain.po.HardInfoPO;
import org.spring.springboot.app.hard.hardinfo.domain.vo.*;
import org.spring.springboot.app.hard.hardinfo.service.IHardInfoService;
import org.spring.springboot.component.annotation.AccessTokenVerify;
import org.spring.springboot.util.ApiIndex;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

@Api(description = "硬件操作接口",tags = ApiIndex.HardInfoController)
@RequestMapping(value = "/api/hardInfo")
@RestController
public class HardInfoController {

    @Autowired
    private IHardInfoService hardinfoervice;
    @Autowired
    private IAdvCombInfoService advCombInfoService;

    @ApiOperation(value = "查询所有硬件")
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    public R<List<HardInfoQueryVO>> findAll(
            @ApiIgnore User user,
            @ApiParam(value = "类型(多个)") @RequestParam(defaultValue = "") String types,
            @ApiParam(value = "是否统计套餐内的设备 0不统计 1统计") @RequestParam(defaultValue = "0") int comb
    ) {
        R result = new R();
        List<HardInfoPO> _list = hardinfoervice.findAll(user.getOfficeId(), types);
        List<HardInfoQueryVO> list = new ArrayList<HardInfoQueryVO>();
        String hardIds = "";
        if (_list != null && _list.size() > 0) {
            for (HardInfoPO po : _list) {
                HardInfoQueryVO vo = new HardInfoQueryVO();
                BeanUtils.copyProperties(po,vo);
                list.add(vo);

                if (StringUtils.isNotBlank(hardIds)) {
                    hardIds += ",";
                }
                hardIds += "'" + po.getId() + "'";
            }
        }
        //需要统计套餐内设备
        if (comb == 1 && StringUtils.isNotBlank(hardIds)) {
            List<AdvCombHardPO> combHardPOS = advCombInfoService.queryInCombHardList(hardIds);
            for (AdvCombHardPO combHardPO : combHardPOS) {
                for (HardInfoQueryVO vo : list) {
                    if (vo.getId().equals(combHardPO.getHardId())) {
                        vo.setIsInComb(1);
                        break;
                    }
                }
            }
        }
        result.setData(list);
        return result;
    }

    @ApiOperation(value = "查询硬件列表-分页")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public R<PageInfo<List<SyshardinfoVO>>> findPageAll(
            @ApiParam(value = "名称", required = false) @RequestParam(defaultValue = "") String name,
            @ApiParam(value = "页数", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam(value = "每页条数", required = true) @RequestParam(defaultValue = "10") int pageSize,
            @ApiParam(value = "类型 5门匝 10公共锁离线 12家庭锁 13公共锁在线 14广告屏", required = true) @RequestParam(defaultValue = "") int type,
            @ApiParam(value = "开始创建时间（例如1993-08-11 或者 1993-08-11 00:00:00）", required = false) @RequestParam(defaultValue = "") String startCreateDate,
            @ApiParam(value = "结束创建时间（例如1993-08-11 或者 1993-08-11 23:59:59）", required = false) @RequestParam(defaultValue = "") String endCreateDate
    ) {
        SyshardinfoSearchVO searchvo=new SyshardinfoSearchVO();  //查询对象
        if(StringUtils.isNotBlank(startCreateDate)){
            if(startCreateDate.indexOf(":")==-1){
                startCreateDate=startCreateDate+" 00:00:00";
            }
        }
        if(StringUtils.isNotBlank(endCreateDate)){
            if(endCreateDate.indexOf(":")==-1){
                endCreateDate=endCreateDate+" 23:59:59";
            }
        }
        searchvo.setStartCreateDate(startCreateDate);
        searchvo.setEndCreateDate(endCreateDate);
        searchvo.setName(name);
        searchvo.setType(type);
        R<PageInfo<List<SyshardinfoVO>>> result = new R<PageInfo<List<SyshardinfoVO>>>();
        List<SyshardinfoVO> _list = hardinfoervice.findPage(searchvo,pageNum, pageSize);
        PageInfo p = new PageInfo(_list);
        result.setData(p);
        return result;
    }




    @ApiOperation(value = "查询代理商硬件列表-分页")
    @RequestMapping(value = "/agent", method = RequestMethod.GET)
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    public R<PageInfo<List<SyshardinfoVO>>> findPageAgent(
            @ApiIgnore User user,
            @ApiParam(value = "名称", required = false) @RequestParam(defaultValue = "") String name,
            @ApiParam(value = "页数", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam(value = "每页条数", required = true) @RequestParam(defaultValue = "10") int pageSize,
            @ApiParam(value = "类型 5门匝 10公共锁离线 12家庭锁 13公共锁在线 14广告屏", required = true) @RequestParam(defaultValue = "") int type,
            @ApiParam(value = "开始创建时间（例如1993-08-11 或者 1993-08-11 00:00:00）", required = false) @RequestParam(defaultValue = "") String startCreateDate,
            @ApiParam(value = "结束创建时间（例如1993-08-11 或者 1993-08-11 23:59:59）", required = false) @RequestParam(defaultValue = "") String endCreateDate
    ) {
        SyshardinfoSearchVO searchvo=new SyshardinfoSearchVO(); //查询对象
        if(StringUtils.isNotBlank(startCreateDate)){
            if(startCreateDate.indexOf(":")==-1){
                startCreateDate=startCreateDate+" 00:00:00";
            }
        }
        if(StringUtils.isNotBlank(endCreateDate)){
            if(endCreateDate.indexOf(":")==-1){
                endCreateDate=endCreateDate+" 23:59:59";
            }
        }
        searchvo.setStartCreateDate(startCreateDate);
        searchvo.setEndCreateDate(endCreateDate);
        searchvo.setName(name);
        searchvo.setType(type);
        R<PageInfo<List<SyshardinfoVO>>> result = new R<PageInfo<List<SyshardinfoVO>>>();
        List<SyshardinfoVO> _list = hardinfoervice.findPageAgent(searchvo,pageNum, pageSize,user.getOfficeId());
        PageInfo p = new PageInfo(_list);
        result.setData(p);
        return result;
    }




    @ApiOperation(value = "根据Id获取单个信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public R<SyshardinfoVO> find(
            @ApiParam(value = "id", required = true) @PathVariable("id") String id
    ) {
        R result = new R();
        SyshardinfoVO vo = hardinfoervice.findSigle(id);
        result.setData(vo);
        return result;
    }

    @ApiOperation(value = "保存信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.POST)
    public R save(
            @ApiParam(value = "对象", required = true) @RequestBody SyshardinfosaveVO syshardinfosaveVO,
            @ApiIgnore User user
    ) {
        return hardinfoervice.saveInfo(syshardinfosaveVO);
    }


    @ApiOperation(value = "修改信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public R update(
            @ApiParam(value = "对象", required = true) @RequestBody SyshardinfoUpdateVO syshardinfoUpdateVO,
            @ApiIgnore User user
    ) {
       return hardinfoervice.updateInfo(syshardinfoUpdateVO);
    }

    @ApiOperation(value = "删除信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public R delete(
            @ApiParam(value = "id", required = true) @PathVariable("id") String id
    ) {
        R result = new R();
        int code = hardinfoervice.delete(id);
        if (code == 0) {
            result.setType(Type.OperError);
            result.setMessage("删除失败");
        }
        return result;
    }

    /**
     *  批量删除
     * @param ids
     */
    @ApiOperation(value="批量删除信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/deletes", method = RequestMethod.DELETE)
    public R deletes(@ApiParam(value = "ids(用,分割)", required = true) @RequestParam("ids") String ids) {
        R result = new R();
        int code = hardinfoervice.deletes(ids);
        if (code == 0) {
            result.setCode(1);
            result.setMessage("刪除失败");
        }
        return result;
    }

}
